﻿/*-----------------------------------------------------------------------------!
! AddressKazImpl class                                                         !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : AddressKazImpl.cs                                                 !
! Function : Implementation of AddressKaz interface                            !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
! Last changed by: Md. Shaiful Islam, May 02 2011                              !
===============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Sadharon;
using Model.Upadan;
using Model.Sohaok;
using System.Data.SqlClient;
using System.Data;

namespace Model.Poribar.AddressPanjeri
{
    class AddressKazImpl : IAddressKaz
    {

        #region AddressKaz Members: Add Method
        /**
         * add : added new address
         * @param object
         * @return Result object
         **/        
        public Result add(object _obj)
        {
            Result result = new Result();
            Address address = new Address();
            DBSadharon dbUtil = new DBSadharon();
            address = (Address)_obj;
            
            SqlConnection conn = new SqlConnection();                            
            try
            {
                conn.ConnectionString = GetConnectionString.Instance();
                // get UniqId for supplier
                string uniqId = dbUtil.getUniqId(conn, "Address", "AD");
                if (uniqId.Equals("falied"))
                {
                    string msg = "Failed to add new address: Error in generating uniqeId. If problem persist " +
                                     "please contact with system administrator";
                    result = Util.setSuccessResult(false, msg);
                }
                else
                {
                    address.Id = uniqId;                
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.CommandType = CommandType.Text;
                    string query = "Insert into Address (Id, Description, PostCode, Country, Telephone, " +
                        "Mobile, Fax, Email, Web, PersonId, Live) Values(" +
                        "'" + Util.filterString(address.Id) + "'," +
                        "'" + Util.filterString(address.Description) + "'," +
                        "'" + Util.filterString(address.PostCode) + "'," +
                        "'" + Util.filterString(address.Country) + "'," +
                        "'" + Util.filterString(address.Telephone) + "'," +
                        "'" + Util.filterString(address.Mobile) + "'," +
                        "'" + Util.filterString(address.Fax) + "'," +
                        "'" + Util.filterString(address.Email) + "'," +
                        "'" + Util.filterString(address.Web) + "'," +
                        "'" + Util.filterString(address.PersonId) + "'," +
                        "'" + "L" + "'" +
                        ")";
                    cmd.CommandText = query;
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        result = Util.setSuccessResult(true, "New address added successfully");
                    }
                    else
                    {
                        result = Util.setSuccessResult(false, "Failed to add (Query failed)");
                    }
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }

        #endregion

        #region AddressKaz member: getById
        ///
        // getById
        // @param personId
        // @return result object
        ///
        public Result getById(string personId)
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Select * from Address Where PersonId='" + personId + "' and Live='L'";
                SqlDataReader addressDataReader = DBSadharon.MakeQuery(query, conn).ExecuteReader();
                if (addressDataReader.HasRows)
                {
                    Address address = new Address();
                    while (addressDataReader.Read())
                    {
                        address.Country = addressDataReader["Country"].ToString();
                        address.Description = addressDataReader["Description"].ToString();
                        address.Email = addressDataReader["Email"].ToString();
                        address.Fax = addressDataReader["Fax"].ToString();
                        address.Id = addressDataReader["Id"].ToString();
                        address.Live = addressDataReader["Live"].ToString();
                        address.Mobile = addressDataReader["Mobile"].ToString();
                        address.PersonId = addressDataReader["PersonId"].ToString();
                        address.PostCode = addressDataReader["PostCode"].ToString();
                        address.Telephone = addressDataReader["Telephone"].ToString();
                        address.Web = addressDataReader["Web"].ToString();
                    }
                    result.ResultObj = address;
                    result.IsSuccess = true;
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get address details: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get address details: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region AddressKaz member: getList
        ///
        // getList
        ///
        public Result getList()
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Select * from Address Where Live='L'";
                SqlDataReader addressDataReader = DBSadharon.MakeQuery(query, conn).ExecuteReader();
                if (addressDataReader.HasRows)
                {
                    List<Address> listAddress = new List<Address>();
                    Address address = new Address();
                    while (addressDataReader.Read())
                    {
                        address = new Address();
                        address.Country = addressDataReader["Country"].ToString();
                        address.Description = addressDataReader["Description"].ToString();
                        address.Email = addressDataReader["Email"].ToString();
                        address.Fax = addressDataReader["Fax"].ToString();
                        address.Id = addressDataReader["Id"].ToString();
                        address.Live = addressDataReader["Live"].ToString();
                        address.Mobile = addressDataReader["Id"].ToString();
                        address.PersonId = addressDataReader["PersonId"].ToString();
                        address.PostCode = addressDataReader["PostCode"].ToString();
                        address.Telephone = addressDataReader["Telephone"].ToString();
                        address.Web = addressDataReader["Web"].ToString();
                        listAddress.Add(address);
                    }
                    result.ResultObj = listAddress;
                    result.IsSuccess = true;
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get address list: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get address list: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region AddressKaz Members: Update Method
        /**
         * updateSupplierDetails : update address information
         * @param object
         * @return Result object
         **/
        public Result update(object _obj)
        {
            Result result = new Result();
            Address address = new Address();
            address = (Address)_obj;
            address.Live = "L";

            SqlConnection conn = new SqlConnection();
            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();                
                string query = "Update Address set " +
                    "Country='" + Util.filterString(address.Country) + "', " +
                    "Description='" + Util.filterString(address.Description) + "', " +
                    "Email='" + Util.filterString(address.Email) + "'," +
                    "Fax='" + Util.filterString(address.Fax) + "', " +
                    "Live='" + Util.filterString(address.Live) + "', " +
                    "PostCode='" + Util.filterString(address.PostCode) + "', " +
                    "Mobile='" + Util.filterString(address.Mobile) + "', " +
                    "Telephone='" + Util.filterString(address.Telephone) + "', " +
                    "Web='" + Util.filterString(address.Web) + "' " +
                    "Where PersonId='" + address.PersonId + "'";
                
                if (DBSadharon.MakeQuery(query, conn).ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "Address updated successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to update address (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to update address: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to update address: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region AddressKaz Members: delete Method
        /**
         * delete : delete address information
         * @param personId
         * @return Result object
         **/
        public Result delete(string personId)
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();
            
            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Update Address set Live='D' Where PersonId='" + personId + "'";

                if (DBSadharon.MakeQuery(query,conn).ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "address deleted successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to delete address (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete address: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete address: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion


    }
}
